package org.kongcloud.core.upload;

import java.util.ArrayList;
import java.util.List;

public class ListTool {
    public static <T> List<List<T>> splitList(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remainder = source.size() % n;  //先计算出余数
        int number = source.size() / n;  //然后是商
        int offset = 0;//偏移量（用以标识加的余数）
        for (int i = 0; i < n; i++) {
            List<T> value;
            if (remainder > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remainder--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>(10);
        for (int i = 0; i < 15; i++) {
            list.add(i + "");
        }
        List<List<String>> result = splitList(list,7);
        for(int i=0;i<result.size();i++){
            System.out.println(result.get(i));
        }
    }

}
